/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servlet;

import com.kamus.Constants;
import com.kamus.manager.FileDbMgr;
import com.kamus.manager.PasanganKataGIMgr;
import com.kamus.manager.PasanganKataIGMgr;
import com.kamus.model.FileDb;
import com.kamus.model.PasanganKata;
import com.util.CommonUtil;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author omega
 */
public class PasanganKataServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter(Constants.ACTION);
        String type = request.getParameter(Constants.TYPE);

        if (type.equals(Constants.GAUL_IND)) {
            if (action.equals(Constants.DELETE)) {
                String[] ids = request.getParameterValues("item");
                if (ids != null) {
                    for (int i = 0; i < ids.length; i++) {
                        PasanganKataGIMgr.getInstance().deleteKata(Integer.parseInt(ids[i]));
                    }
                }

                RequestDispatcher rd = request.getRequestDispatcher("KosakataListGI.jsp");
                rd.forward(request, response);
            } else if (action.equals(Constants.CREATE)) {
                ArrayList<PasanganKata> list = PasanganKataGIMgr.getInstance().getKatas();
//                ArrayList<PasanganKata> listResult = new ArrayList<PasanganKata>();
                PasanganKata pKata = null, pKataTmp = null;
                int i = 0;
                StringBuffer result = new StringBuffer();
                for (Iterator<PasanganKata> it = list.iterator(); it.hasNext();) {
                    if (i == 0) {
                        pKataTmp = it.next();
                    } else {
                        pKata = it.next();
                        if(pKata.getGaul().equals(pKataTmp.getGaul())){
                            pKata.setIndonesia(pKata.getIndonesia()+";"+pKataTmp.getIndonesia());
                            pKataTmp = pKata;
                        }else{
//                            listResult.add(pKata);
                            result.append(pKataTmp.getGaul()).append("=").append(pKataTmp.getIndonesia()).append("\r\n");
                            pKataTmp=pKata;
                        }
                        if(i==list.size()-1){
//                            listResult.add(pKataTmp);
                            result.append(pKataTmp.getGaul()).append("=").append(pKataTmp.getIndonesia());
                        }
                    }
                    i++;
                }
                ////////////////////////// masukkan sorting mu ndi ////////////////////
                 /**
                 * sementara nilai result =
                 * eike=aku;saya
                 * endang=enak
                 */
                String [] arr = result.toString().split("\r\n");
                ////////////////////////// end ///////////////////////////////////////
                SimpleDateFormat sdf = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
                Date now = new Date();
                String filename = "db_gi_"+sdf.format(now);
                
                FileDb fileDb = new FileDb();
                fileDb.setNama(filename);
                fileDb.setTanggal(now);
                fileDb.setType(Constants.GAUL_IND);
                FileDbMgr.getInstance().insertFile(fileDb);

                CommonUtil.createGZip(Constants.PATH+filename+".dat",result.toString());
                RequestDispatcher rd = request.getRequestDispatcher("KosakataListGI.jsp");
                rd.forward(request, response);
            }
        } else if (type.equals(Constants.IND_GAUL)) {
            if (action.equals(Constants.DELETE)) {
                String[] ids = request.getParameterValues("item");
                if (ids != null) {
                    for (int i = 0; i < ids.length; i++) {
                        PasanganKataIGMgr.getInstance().deleteKata(Integer.parseInt(ids[i]));
                    }
                }

                RequestDispatcher rd = request.getRequestDispatcher("KosakataListIG.jsp");
                rd.forward(request, response);
            } else if (action.equals(Constants.CREATE)) {
                ArrayList<PasanganKata> list = PasanganKataIGMgr.getInstance().getKatas();
                PasanganKata pKata = null, pKataTmp = null;
                int i = 0;
                StringBuffer result = new StringBuffer();
                for (Iterator<PasanganKata> it = list.iterator(); it.hasNext();) {
                    if (i == 0) {
                        pKataTmp = it.next();
                    } else {
                        pKata = it.next();
                        if(pKata.getIndonesia().equals(pKataTmp.getIndonesia())){
                            pKata.setGaul(pKata.getGaul()+";"+pKataTmp.getGaul());
                            pKataTmp = pKata;
                        }else{
                            result.append(pKataTmp.getIndonesia()).append("=").append(pKataTmp.getGaul()).append("\r\n");
                            pKataTmp=pKata;
                        }
                        if(i==list.size()-1){
                            result.append(pKataTmp.getIndonesia()).append("=").append(pKataTmp.getGaul());
                        }
                    }
                    i++;
                }
                ////////////////////////// masukkan sorting mu ndi ////////////////////
                /**
                 * sementara nilai result =
                 * aku=eike;gue
                 * enak=endang
                 */
                String [] arr = result.toString().split("\r\n");
                ////////////////////////// end ///////////////////////////////////////
                SimpleDateFormat sdf = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
                Date now = new Date();
                String filename = "db_ig_"+sdf.format(now);

                FileDb fileDb = new FileDb();
                fileDb.setNama(filename);
                fileDb.setTanggal(now);
                fileDb.setType(Constants.IND_GAUL);
                FileDbMgr.getInstance().insertFile(fileDb);

                CommonUtil.createGZip(Constants.PATH+filename+".dat",result.toString());
                RequestDispatcher rd = request.getRequestDispatcher("KosakataListGI.jsp");
                rd.forward(request, response);
            }
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
